package hu.szyrt.bug;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import hu.szyrt.bug.adapters.PlacesListAdapter;
import hu.szyrt.bug.db.DBHandler;
import hu.szyrt.bug.models.PlaceItemBase;
import hu.szyrt.bug.utils.Assets;
import hu.szyrt.bug.views.SearchEditText;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class ListActivity extends Activity {

	Assets assets;
	DBHandler dbHandler;
	
	ListView listView;
	PlacesListAdapter listAdapter;
	
	Button backButton;
	
	SearchEditText searchEditText;
	TextWatcher placeSearchTextWatcher;
	Timer timer = new Timer();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category_list);
				
		assets = new Assets(this);
		dbHandler = new DBHandler(this);
		
		backButton = (Button)findViewById(R.id.backButton);
		backButton.setCompoundDrawables(null, null, null, null);
		backButton.setPadding(10, 0, 0, 0);
		backButton.setText(R.string.list);		
		
		listAdapter = new PlacesListAdapter(assets, dbHandler.loadPlaces(null, false, null));
		
		listView = (ListView)findViewById(R.id.listView);
		listView.setAdapter(listAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> a, View v, int i, long l) {
				PlaceItemBase pib = (PlaceItemBase)listAdapter.getItem(i);
				showPlace(pib);
			}
		});
		listView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				searchEditText.clearFocus();
				return false;
			}
		});
		
		// Kereső mező
		searchEditText = (SearchEditText)findViewById(R.id.search);
		if(placeSearchTextWatcher != null){
			searchEditText.removeTextChangedListener(placeSearchTextWatcher);
			placeSearchTextWatcher = null;
		}
		placeSearchTextWatcher = new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void afterTextChanged(Editable s) {
				final String searchTerms = s.toString().trim();
				timer.cancel();
				timer.purge();
				timer = new Timer();
				timer.schedule(new TimerTask() {
					public void run() {
						final List<PlaceItemBase> places = searchTerms.length() > 1 ? 
								dbHandler.loadPlaces(null, false, searchTerms) :
								dbHandler.loadPlaces(null, false, null);
						runOnUiThread(new Runnable() {
							public void run() {
								listAdapter.setTerms(searchTerms.length() > 1 ? searchTerms : null);
								listAdapter.changeList(places);
							}
						});
					}
				}, 700);
			}
		};
		searchEditText.setText(null);
		searchEditText.addTextChangedListener(placeSearchTextWatcher);

	}
	
	
	
	private void showPlace(PlaceItemBase place){
		Intent i = new Intent(this, PlaceActivity.class);
		i.putExtra("place_id", place.getId());
		startActivity(i);
	}
	
	
}
